package com.gy.controller.common;


import com.gy.result.Result;
import com.gy.service.ImageMergeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/file/merge")
@Api(tags = "图片合并管理")
@CrossOrigin
@Slf4j
public class ImageMergeController {

    @Autowired
    private ImageMergeService imageMergeService;

    /**
     * 将多个图片合并为PDF
     * @param mergeData 合并数据，包含图片URL列表、封面数据、目录数据等
     * @return PDF文件的二进制数据
     */
    @PostMapping("/imagesToPdf")
    @ApiOperation("将图片序列合并为PDF")
    public ResponseEntity<byte[]> mergeImagesToPdf(@RequestBody Map<String, Object> mergeData) {
        try {
            // 从请求体中提取数据
            @SuppressWarnings("unchecked")
            List<String> imageUrls = (List<String>) mergeData.get("imageUrls");
            @SuppressWarnings("unchecked")
            Map<String, Object> coverData = (Map<String, Object>) mergeData.get("coverData");
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> tableOfContents = (List<Map<String, Object>>) mergeData.get("tableOfContents");
            Boolean showCover = (Boolean) mergeData.getOrDefault("showCover", true);
            Boolean showTableOfContents = (Boolean) mergeData.getOrDefault("showTableOfContents", true);

            // 调用服务层合并图片为PDF
            byte[] pdfBytes = imageMergeService.mergeImagesToPdf(
                imageUrls,
                coverData,
                tableOfContents,
                showCover,
                showTableOfContents
            );

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_PDF);
            headers.setContentDispositionFormData("attachment", "merged.pdf");
            headers.setContentLength(pdfBytes.length);

            return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);

        } catch (Exception e) {
            log.error("合并图片为PDF失败", e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 批量获取多个PDF文件的图片列表
     * @param pdfPaths PDF文件路径列表
     * @return 所有图片URL的合并列表
     */
    @PostMapping("/batchGetImages")
    @ApiOperation("批量获取PDF图片列表")
    public Result<Map<String, Object>> batchGetPdfImages(@RequestBody List<String> pdfPaths) {
        try {
            Map<String, Object> result = imageMergeService.batchGetPdfImages(pdfPaths);
            return Result.success(result);
        } catch (Exception e) {
            log.error("批量获取PDF图片失败", e);
            return Result.error("获取图片列表失败：" + e.getMessage());
        }
    }
}
